package com.ti.srpglife.entity;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;

public class Unit extends Actor {
	//factions
	public static final int NEUTRAL = 0;
	public static final int PLAYER = 1;
	public static final int ENEMY = 2;
	
	public int faction;
	public int id;
	private float speed = 100;
	
	private Sprite sprite;
	private Sprite halo;
	
	private Unit target;
	
	float x,y;
	
	boolean selected;
	private Vector2 moveto;
	
	public Unit()
	{
		sprite = new Sprite(new Texture("img/badass.png"),0,0,64,64); 
		halo = new Sprite(new Texture("img/uselect.png"),0,0,64,64);
		x = y = 64;
		selected = false;
		faction = PLAYER;
		halo.setColor(Color.CLEAR);
		id = 1;
		target = null;
		moveto = null;
	}
	
	public Sprite getSprite() 
	{
		return sprite;
	}
	
	public Sprite getHalo()
	{
		return halo;
	}
	
	public void translate(float x, float y)
	{
		super.translate(x, y);
		sprite.translate(x, y);
		halo.translate(x, y);
		this.x = x;
		this.y = y;
		this.setWidth(64);
		this.setHeight(64);
	}

	public void select() 
	{
		selected = true;
		halo.setColor(Color.BLUE);
	}
	
	public void unselect()
	{
		selected = false;
		halo.setColor(Color.CLEAR);
	}
	
	public void draw(SpriteBatch batch, float parentAlpha)
	{
		super.draw(batch, parentAlpha);
		halo.draw(batch);
		sprite.draw(batch);
	}

	public void setTarget(Unit u) 
	{
		target = u;
		//add attack action
	}

	public void commandMove(float x, float y) 
	{
		target = null;
		moveto = new Vector2(x,y);
	}
	
	public void act(float delta)
	{
		if(moveto != null)
		{
			float shift = delta*speed;
			Vector2 pos = new Vector2(x,y);
			
			Vector2 dir = moveto.sub(pos);
			dir = dir.div(dir.len());
			dir = dir.scl(shift);
			
			pos = pos.add(dir);
			this.translate(pos.x, pos.y);
			this.halo.translate(x, y);
			this.sprite.translate(x, y);
		}
		if(target != null)
		{
			
		}
	}
}
